package com.woniu.outpatient.controller;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.web.page.TableDataInfo;
import com.woniu.outpatient.domain.Drug;
import com.woniu.outpatient.service.IDoctorService;
import com.woniu.outpatient.service.IDrugService;
import com.woniu.outpatient.service.IDruglistService;
import com.woniu.user.domain.Doctor;
import com.woniu.user.domain.Patient;
import com.woniu.vo.ZhaiVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.woniu.outpatient.domain.Prescription;
import com.woniu.outpatient.service.IPrescriptionService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;

/**
 * 药单流水表Controller
 *
 * @author wolf-six
 * @date 2023-02-15
 */
@RestController
@RequestMapping("/prescription")
public class PrescriptionController extends BaseController
{
    @Autowired
    private IPrescriptionService prescriptionService;



    @Autowired
    private IDoctorService iDoctorService;

    @Autowired
    private IDruglistService druglistService;

    /**
     * ctrl + alt m 提取出的方法，作用是根据id匹配患者医生
     */
    private Prescription extracted(Prescription prescription1) {
        Long doctorId = prescription1.getDoctorId();
        Doctor doctor = iDoctorService.selDoctorById(doctorId);
        prescription1.setDoctorName(doctor.getDoctorName());
        Long patientId = prescription1.getPatientId();
        Patient nameById = iDoctorService.getNameById(patientId);
        prescription1.setPatientName(nameById.getPatientName());
        Long prescriptionId = prescription1.getPrescriptionId();
        List<ZhaiVO> zhaiVOS = druglistService.selDrugPriceAndDrugNum(prescriptionId);
        double sum = 0.0;
        for (ZhaiVO zhaiVO : zhaiVOS) {
            long drugListNum = zhaiVO.getDrugListNum();
            BigDecimal drugPrice = zhaiVO.getDrugPrice();
            double v = drugPrice.doubleValue();
            sum += drugListNum * v;

        }
        BigDecimal bigDecimal = BigDecimal.valueOf(sum);
        prescription1.setPrescriptionMoney(bigDecimal);
        System.out.println("sum = " + bigDecimal);
        return prescription1;
    }

    /**
     * 查询药单流水表列表
     */
    @RequiresPermissions("outpatient:prescription:list")
    @GetMapping("/list")
    public TableDataInfo lists(Prescription prescription) throws InvocationTargetException, IllegalAccessException {
        startPage();
        System.out.println("进来了");
        List<Prescription> list = prescriptionService.selectPrescriptionList(prescription);
        System.out.println("出来了");
        for (Prescription prescription1 : list) {
            extracted(prescription1);

        }
        System.out.println("完成");

       return getDataTable(list);
    }



    /**
     * 导出药单流水表列表
     */
    @RequiresPermissions("outpatient:prescription:export")
    @Log(title = "药单流水表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Prescription prescription)
    {
        List<Prescription> list = prescriptionService.selectPrescriptionList(prescription);
        ExcelUtil<Prescription> util = new ExcelUtil<Prescription>(Prescription.class);
        util.exportExcel(response, list, "药单流水表数据");
    }

    /**
     * 获取药单流水表详细信息
     */
    @RequiresPermissions("outpatient:prescription:query")
    @GetMapping(value = "/{prescriptionId}")
    public AjaxResult getInfo(@PathVariable("prescriptionId") Long prescriptionId)
    {
        Prescription prescription = prescriptionService.selectPrescriptionByPrescriptionId(prescriptionId);
        Prescription extracted = extracted(prescription);
        return success(extracted);
    }

    /**
     * 新增药单流水表
     */
    @RequiresPermissions("outpatient:prescription:add")
    @Log(title = "药单流水表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Prescription prescription)
    {
        int rows = prescriptionService.insertPrescription(prescription);
        Long prescriptionId = prescription.getPrescriptionId();

        return success(prescriptionId);
    }

    /**
     * 修改药单流水表
     */
    @RequiresPermissions("outpatient:prescription:edit")
    @Log(title = "药单流水表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Prescription prescription)
    {
        return toAjax(prescriptionService.updatePrescription(prescription));
    }

    /**
     * 删除药单流水表
     */
    @RequiresPermissions("outpatient:prescription:remove")
    @Log(title = "药单流水表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{prescriptionIds}")
    public AjaxResult remove(@PathVariable Long[] prescriptionIds)
    {
        return toAjax(prescriptionService.deletePrescriptionByPrescriptionIds(prescriptionIds));
    }

    @GetMapping("/drugCost/{patientId}")
    public BigDecimal drugCost(@PathVariable("patientId") Long patientId){
        return prescriptionService.drugCost(patientId);
    }
}
